package com.baosight.sczx.zm.cl.service;
import java.util.HashMap;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.cl.RoundCheckWorkNote;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cl.domain.ZMCLTernateNeedFeedback_x;

/**
 * 三违反馈模块
 * 待反馈三违记录
 * <p>Title: ServiceZMCL81.java</p>
 * <p>Description: 待反馈三违记录：待反馈三违记录服务类</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2011-4-22下午      高强  Create
 *</pre>
 */
public class ServiceZMCL81 extends ServiceEPBase { 
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 当前页面托管加载初始化操作</p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-22下午      高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		this.modifyStatus();//查收所有三违记录
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.classify"));//三违分类
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.sourceClassify"));//三违来源
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cl.department", "orgCode", "orgName", false));//三违单位
		outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.cl.responsibilityPeople", "empCode", "empName", false));//三违反馈人
		//outInfo.addBlock(WorkingFace.getAllWorkingFace("cmes.cl.workFace", false));//所在工作面
		//outInfo.addBlock(RoadWay.getAllRoadWay("cmes.cl.roadWay", false));//所在巷道
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cl.workNum", false));//三违班次
		outInfo.addBlock(RoundCheckWorkNote.getAllNoteCode("cmes.cl.workNote", true));//巡检工单
		outInfo.set("fdFeedbackPeople", UserSession.getInSessionProperty("UserCname"));//默认反馈人为当前登录人
		return outInfo;
	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询未反馈记录</p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-22下午    高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "feedbacking", "yes", true);
		StaticTools.setQueryParameters(inInfo, "fdResponseDepartmentId",UserSession.getInSessionProperty("orgCode").toString(), true);//查询所有安全管理单位下待反馈的三违记录
		EiInfo outInfo = super.query(inInfo, "ZMCLTernateNeedFeedback_x.query", new ZMCLTernateNeedFeedback_x());
		return outInfo;
	}
	
	/**
	 * 三违反馈
	 * <p>Function: insert</p>
	 * <p>Description: 对未反馈的记录进行反馈操作</p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2011-4-18 上午 高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		String fdKeyId = TablePk.getInstance().getNextValue("T_CL_TERNATE_FEEDBACK");//三违反馈ID
		String fdTernateRecordId = inInfo.getBlock("result").getCellStr(0, "fdTernateRecordId");//三违记录ID
		String fdFeedbackPeopleId = inInfo.getBlock("result").getCellStr(0, "fdFeedbackPeopleId");//反馈人ID
		
		inInfo.setCell("result", 0, "fdKeyId", fdKeyId);//ID
		inInfo.setCell("result", 0, "fdFeedbackDate", StaticTools.getDate());
		inInfo.getBlock("result").setCell(0, "fdCreateTime", StaticTools.getDateTime());//创建时间
		inInfo.getBlock("result").setCell(0, "fdFeedbackPeople", fdFeedbackPeopleId);//反馈人
		
		EiInfo outInfo = super.insert(inInfo, "ZMCLTernateFeedback.insert");//新增反馈记录
		inInfo.setCell("result", 0, "fdKeyId", fdTernateRecordId);//三违记录ID
		super.update(inInfo, "ZMCLTernateRecord.modifyStop");//更新三违记录
		return outInfo;
	}
	
	/**
	 * 修改状态 为已查收
	 * <p>Function: modifyStatus</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-7上午05:26:05 严洪浩
	 * </p>
	 *
	 */
	private int modifyStatus() {
		if(null == UserSession.getInSessionProperty("orgCode")) {
			return -1;
		}

		try {
			String fdDepartmentId = UserSession.getInSessionProperty("orgCode").toString();
			Map<String, String> _argMap = new HashMap<String, String>();
			_argMap.put("fdResponseDepartmentId", fdDepartmentId);
			
			int _result = dao.update("ZMCLTernateRecord.modifyChashou", _argMap);
			return _result;
		} catch (Exception e) {
			System.out.println("修改状态 为未反馈异常：" + e.getMessage());
			return -1;
		}
	}
}
